import 'dart:math';

import 'package:aihu_getx_app/utils/constants.dart';
import 'package:aihu_getx_app/widgets/common_image.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../controllers/splash_controller.dart';

class SplashView extends GetView<SplashController> {
  const SplashView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        body: Container(
          width: Get.width,
          height: Get.height,
          padding: EdgeInsets.only(
            top: ScreenUtil().statusBarHeight,
          ),
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage(Constants.splashBgImage),
              fit: BoxFit.cover,
            ),
          ),
          child: Stack(
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(top: Get.height * 0.02),
                  child: CommonImage(
                    Constants.splashLogoImage,
                    width: min(Get.width, Get.height) * 0.4,
                  ),
                ),
              ),
              Positioned(
                bottom: 20,
                height: Get.height * 0.7,
                width: Get.width,
                child: CarouselSlider(
                  carouselController: controller.carouselController,
                  options: CarouselOptions(
                    height: 380,
                    aspectRatio: 16 / 9,
                    viewportFraction: 0.7,
                    enlargeCenterPage: true,
                    onPageChanged: (index, reason) {
                      controller.current.value = index;
                      controller.reset();
                    },
                  ),
                  items: Constants.clientBaseConfigData.map(
                    (item) {
                      return Builder(
                        builder: (BuildContext context) {
                          return GestureDetector(
                            onTap: () {
                              controller.switchPage(item);
                            },
                            child: Container(
                              width: Get.width,
                              decoration: BoxDecoration(
                                color: Colors.white.withOpacity(0.5),
                                borderRadius: BorderRadius.circular(20),
                              ),
                              child: SingleChildScrollView(
                                child: Column(
                                  children: [
                                    Container(
                                      height: 320,
                                      margin: const EdgeInsets.only(
                                        top: 30,
                                      ),
                                      clipBehavior: Clip.hardEdge,
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      child: CommonImage(
                                        item.image,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          );
                        },
                      );
                    },
                  ).toList(),
                ),
              ),
              Positioned(
                bottom: Get.height * 0.3 + 20,
                left: 10,
                child: IconButton(
                  onPressed: () {
                    controller.reset(offset: -1);
                  },
                  icon: const Icon(
                    Icons.chevron_left,
                    size: 80,
                  ),
                  style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all(Colors.white),
                    backgroundColor: MaterialStateProperty.all(
                        Colors.black.withOpacity(0.2)),
                  ),
                ),
              ),
              Positioned(
                bottom: Get.height * 0.3 + 20,
                right: 10,
                child: IconButton(
                  onPressed: () {
                    controller.reset(offset: 1);
                  },
                  icon: const Icon(
                    Icons.chevron_right,
                    size: 80,
                  ),
                  style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all(Colors.white),
                    backgroundColor: MaterialStateProperty.all(
                        Colors.black.withOpacity(0.2)),
                  ),
                ),
              ),
              Positioned(
                right: 30,
                bottom: 30,
                child: TextButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.black38),
                    foregroundColor: MaterialStateProperty.all(Colors.white),
                    shape: MaterialStateProperty.all(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20),
                      ),
                    ),
                  ),
                  onPressed: () {
                    controller.switchPage(null);
                  },
                  child: Text(
                      "${controller.count}秒后跳到${Constants.clientBaseConfigData[controller.current.value].title}"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
